package com.fhnew.app.ui.dialogactivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.ResVerifyPhone;
import com.fhnew.app.model.network.entity.req.changeNikeNameBean;
import com.fhnew.app.model.network.entity.req.update_PwdBean;
import com.fhnew.app.util.CheckView;
import com.fhnew.app.util.Md5;
import com.fhnew.app.util.SYSOUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class WithdrawDialog extends Activity {

    ImageView close;
    TextView titletext;
    EditText code_imget;
    CheckView code_img;
    LinearLayout tuxinglayout;
    EditText pass;
    EditText money;
    EditText etsms;
    TextView smsendbt;
    LinearLayout smslayout;
    LinearLayout commitlayout;
    TextView layout3;
    TextView nextbt;
    TextView nextbt2;
    private boolean flag;
    private String bid;
    String[] code_reslut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw_dialog);
        bid = getIntent().getStringExtra("bid");
        close = findViewById(R.id.close);
        money = findViewById(R.id.money);
        pass = findViewById(R.id.pass);
        titletext = findViewById(R.id.titletext);
        code_imget = findViewById(R.id.code_imget);
        code_img = findViewById(R.id.code_img);
        tuxinglayout = findViewById(R.id.tuxinglayout);
        etsms = findViewById(R.id.etsms);
        smsendbt = findViewById(R.id.smsendbt);
        smslayout = findViewById(R.id.smslayout);
        commitlayout = findViewById(R.id.commitlayout);
        layout3 = findViewById(R.id.layout3);
        nextbt = findViewById(R.id.nextbt);
        nextbt2 = findViewById(R.id.nextbt2);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        getisVerifyPhone();
        code_reslut = code_img.getValidataAndSetImage();//生成验证码
        code_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                code_reslut = code_img.getValidataAndSetImage();//生成验证码
            }
        });
        smsendbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getisVerifyPhone();
            }
        });
        nextbt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //短信是否打开了
    private void getisVerifyPhone() {
        HttpJsonUtils.getHttpApiInstance()
                .getVerifyPhone(Constants.getuseAdress() + "api/m/bank/isVerifyPhone", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResVerifyPhone>() {
                    @Override
                    protected void onCustomNext(ResVerifyPhone resVerifyPhone) {
                        if (resVerifyPhone.code == 200 && resVerifyPhone.status) {
                            //短信验证
                            tuxinglayout.setVisibility(View.GONE);
                            smslayout.setVisibility(View.VISIBLE);
                            Toast.makeText(WithdrawDialog.this, "短信已发送至您" + resVerifyPhone.data.phone + "的手机，请注意查收", Toast.LENGTH_LONG).show();
                            getdrawSmsCode();
                            nextbt.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (TextUtils.isEmpty(money.getText().toString().trim())) {
                                        Toast.makeText(WithdrawDialog.this, "请输入取款金额", Toast.LENGTH_SHORT).show();
                                        return;
                                    }
                                    if (TextUtils.isEmpty(pass.getText().toString().trim())) {
                                        Toast.makeText(WithdrawDialog.this, "请输入取款密码", Toast.LENGTH_SHORT).show();
                                        return;
                                    }
                                    if (TextUtils.isEmpty(etsms.getText().toString().trim())) {
                                        Toast.makeText(WithdrawDialog.this, "请输入您收到的验证码", Toast.LENGTH_SHORT).show();
                                        return;
                                    }


                                    withdrawSubmit(money.getText().toString(), pass.getText()
                                            .toString(), etsms.getText().toString());

                                }
                            });

                        } else {
                            //图形验证
                            tuxinglayout.setVisibility(View.VISIBLE);
                            smslayout.setVisibility(View.GONE);
                            nextbt.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String yanzheng = code_reslut[0] + code_reslut[1] + code_reslut[2] + code_reslut[3];
                                    if (TextUtils.isEmpty(money.getText().toString().trim())) {
                                        Toast.makeText(WithdrawDialog.this, "请输入取款金额", Toast.LENGTH_SHORT).show();
                                        return;
                                    }
                                    if (TextUtils.isEmpty(pass.getText().toString().trim())) {
                                        Toast.makeText(WithdrawDialog.this, "请输入取款密码", Toast.LENGTH_SHORT).show();
                                        return;
                                    }
                                    if (TextUtils.isEmpty(code_imget.getText().toString().trim())) {
                                        Toast.makeText(WithdrawDialog.this, "请输入验证码", Toast.LENGTH_SHORT).show();
                                        return;
                                    }
                                    if (code_imget.getText().toString().trim().equals(yanzheng)) {
                                        withdrawSubmit(money.getText().toString().trim(), pass.getText()
                                                .toString().trim(), "");
                                    } else {
                                        Toast.makeText(WithdrawDialog.this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
                                        return;
                                    }


                                }
                            });


                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    private void getdrawSmsCode() {
        HttpJsonUtils.getHttpApiInstance()
                .getdrawSmsCode(Constants.getuseAdress() + "api/m/bank/drawSmsCode", UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<changeNikeNameBean>() {
                    @Override
                    protected void onCustomNext(changeNikeNameBean resVerifyPhone) {
                        if (resVerifyPhone.getCode() == 200) {
                            //成功
                            timer.start();

                        } else {
                            timer.cancel();

                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }

    CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            smsendbt.setText((millisUntilFinished / 1000) + "S后重可发");
            smsendbt.setEnabled(false);
        }

        @Override
        public void onFinish() {
            smsendbt.setEnabled(true);
            smsendbt.setText("获取验证码");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    //短信取款
    private void withdrawSubmit(String empty, String b, String empty1) {
        HttpJsonUtils.getHttpApiInstance()
                .withdrawSubmit(Constants.getuseAdress() + "api/m/bank/withdrawSubmit.do",
                        bid + "", empty, Md5.toMd5(b), empty1, UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<update_PwdBean>() {
                    @Override
                    protected void onCustomNext(update_PwdBean bankList) {
                        if (bankList.getCode() == 0) {
                            commitlayout.setVisibility(View.GONE);
                            nextbt.setVisibility(View.GONE);
                            layout3.setVisibility(View.VISIBLE);
                            nextbt2.setVisibility(View.VISIBLE);
                        } else {
                            Toast.makeText(WithdrawDialog.this, "" + bankList.getMsg(), Toast.LENGTH_LONG).show();
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });

    }
}
